<?php
/**
 * Created by PhpStorm.
 * User: qingqing.xie
 * Date: 2018/4/26
 * Time: 10:05
 */

namespace app\api\controller;

use app\data\Data;
//use Elasticsearch\Client;
use app\api\model\Client;

class Keyword extends Index
{


//    public function __construct()
//    {
//        require '../../vendor/autoload.php';
//        $params['hosts'] = array(
//            '127.0.0.1:9200'
//        );
//        $this->client = new \Elasticsearch\Client($params);
//    }


    public function index()
    {
        $Client = new Client;
        $form = $Client->get_ip_from();
        $where['ip'] = $form['ip'];
        $res = db('keyword')->where($where)->order('count desc')->limit(4)->select();
        if ($res) {
            $this->_jsonp('200', '成功', $res);
        } else {
            $res = db('keyword')->order('count desc')->limit(4)->select();
            $this->_jsonp('200', '成功', $res);
        }
    }

    public function keyword()
    {
        $keyword = trim(input('get.keyword'));
        $Client = new Client;
        $form = $Client->get_ip_from();
        $where['keyword'] = $keyword;
        $where['ip'] = $form['ip'];
        $name = db('keyword')->where($where)->find();
        if ($name) {
            $where1['id'] = $name['id'];
            db('keyword')->where($where1)->setInc('count');
        } else {
			if($keyword){
				$data['ip'] = $form['ip'];
				$data['time'] = time();
				$data['keyword'] = $keyword;
				db('keyword')->insert($data);
			}
        }
    }

    /**
     * 全部模糊搜索
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search_work()
    {
        $keyword = trim(input('get.keyword'));

//        $params['index'] = 'zgh';
//        $params['type'] = 'video';
//        $params['body']['query']['match']['title'] = '专';
//        $elastic = $this->client->search($params);

        $text = trim(input('get.text'));


        if ($text == '服务市场') {
            $where['w.state'] = 1;
            $where['w.status'] = 1;
            $where['w.goods_name'] = array('like', "%$keyword%");
            $where['i.status'] = 2;
            $where['i.static'] = 1;
            $join[0] = '__SHOP_INFORMATION__ i';
            $join[1] = 'w.s_id=i.id';
            $join12[0] = "__SERVICE_TYPE__ m";
            $join12[1] = 'm.id = w.classify';
            $fields = 'w.id,w.rate,w.s_id,w.volume as value,w.price as store,w.goods_name as username,w.add_time as time,w.pic as image,i.name';
            $res['market'] = db('service_market')->where($where)->limit(20)->alias('w')->field($fields)->join($join[0], $join[1])->join($join12[0], $join12[1])->select();
            if ($res['market'] == null) {
                $where8['a.username|m.market_name'] = array('like', "%$keyword%");
                $where8['a.status'] = 1;
                $where8['a.static'] = 2;
                $where8['c.status'] = 2;
                $where8['c.static'] = 1;
                $where8['a.is_show'] = 1;
                $join8[0] = "__SHOP_INFORMATION__ c";
                $join8[1] = 'a.name = c.id';
                $join9[0] = "__MARKET_TRADING_CATEGORY__ m";
                $join9[1] = 'm.id = a.category_id';
                $field8 = "a.id,a.value,a.name as s_id,a.store,a.username,a.time,a.image,c.name,c.static,c.status";
                $alias = 'a';
                $res['market'] = db('market_trading')->limit(20)->alias($alias)->field($field8)->join($join8[0], $join8[1])->join($join9[0], $join9[1])->where($where8)->select();
                $res['text'] = '交易市场';
                dump($res['market']);
                return;

            } else {
                $res['text'] = '服务市场';
            }
            //            dump(db('service_market')->getLastSql());
        } else {
            $where['a.username|m.market_name'] = array('like', "%$keyword%");
            $where['a.status'] = 1;
            $where['a.static'] = 2;
            $where8['a.is_show'] = 1;
            $where['c.status'] = 2;
            $where['c.static'] = 1;
            $join[0] = "__SHOP_INFORMATION__ c";
            $join[1] = 'a.name = c.id';
            $join10[0] = "__MARKET_TRADING_CATEGORY__ m";
            $join10[1] = 'm.id = a.category_id';
            $field = "a.id,a.value,a.name as s_id,a.store,a.username,a.time,a.image,c.name,c.static,c.status";
            $alias = 'a';
            $res['market'] = db('market_trading')->limit(20)->alias($alias)->field($field)->join($join[0], $join[1])->join($join10[0], $join10[1])->where($where)->select();
            if ($res['market'] == null) {
                $where7['w.state'] = 1;
                $where['w.status'] = 1;
                $where7['w.goods_name|m.t_name'] = array('like', "%$keyword%");
                $where7['i.status'] = 2;
                $where7['i.static'] = 1;
                $join7[0] = '__SHOP_INFORMATION__ i';
                $join7[1] = 'w.s_id=i.id';
                $join11[0] = "__SERVICE_TYPE__ m";
                $join11[1] = 'm.id = w.classify';
                $fields7 = 'w.id,w.rate,w.s_id,w.volume as value,w.price as store,w.goods_name as username,w.add_time as time,w.pic as image,i.name';
                $res['market'] = db('service_market')->where($where7)->limit(20)->alias('w')->field($fields7)->join($join7[0], $join7[1])->join($join11[0], $join11[1])->select();
                $res['text'] = '服务市场';
            } else {
                $res['text'] = '交易市场';
            }
//            dump(db('market_trading')->getLastSql());
        }


        $where1['n.static'] = 1;
        $where1['n.status'] = 2;
        $where1['n.name|s.shop_name'] = array('like', "%$keyword%");
        $field1 = "n.id,n.username,n.uid as user_id,n.name,n.shop_volume,n.image,n.scope,y.shop_name";
        $join2[0] = "__SHOP_CATEGORY__ y";
        $join2[1] = "y.id = n.shop_id";
        $join13[0] = "__SHOP_CATEGORY__ s";
        $join13[1] = "s.id = n.shop_id";
        $res['info'] = db('shop_information')->alias('n')->join($join2[0], $join2[1])->join($join13[0], $join13[1])->where($where1)->limit(6)->field($field1)->select();
//        dump(db()->getLastSql());
        foreach ($res['info'] as $key => $value) {
            $trading_count = db('market_trading')->where('status', 1)->where('name', $value['id'])->count();
            $service_count = db('service_market')->where('state', 1)->where('s_id', $value['id'])->count();
            $res['info'][$key]['count'] = $trading_count + $service_count;
        }

        $where2['a.status'] = 1;
        $where2['a.title|v.name'] = array('like', "%$keyword%");
        $join14[0] = "__VIDEO_TYPE__ v";
        $join14[1] = "v.tid = a.type";
        $res['video'] = db('video_list')->alias('a')->join($join14[0], $join14[1])->where($where2)->limit(8)->select();
//        dump(db()->getLastSql());
        $join1[0] = "__TALENT_TYPE__ t";
        $join1[1] = "t.id = a.talentrole";
        $where3['a.status'] = 1;
        $where3['a.sheet'] = 1;
        $where3['a.talentname|t.rolename'] = array('like', "%$keyword%");
        $filed1 = 'a.*,t.rolename';
        $res['personnel'] = db('talent')->alias('a')->field($filed1)->join($join1[0], $join1[1])->limit(6)->where($where3)->select();
//        dump(db()->getLastSql());

        $join2[0] = "__TALENT_TYPE__ t";
        $join2[1] = "t.id = a.talentrole";
        $where4['a.status'] = 1;
        $where4['a.sheet'] = 2;
        $where4['a.talentname|t.rolename'] = array('like', "%$keyword%");
        $filed = "a.*,t.rolename";
        $res['teachers'] = db('talent')->alias('a')->field($filed)->limit(6)->join($join2[0], $join2[1])->where($where4)->select();
//        dump(db()->getLastSql());

        $where5['a.status'] = 1;
        $where5['a.title|g.name|a.content'] = array('like', "%$keyword%");
        $join15[0] = "__GOVERNMENT_TYPE__ g";
        $join15[1] = "g.id = a.type";
        $res['government'] = db('government_work')->alias('a')->where($where5)->join($join15[0], $join15[1])->limit(5)->select();
//        dump(db()->getLastSql());
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 政务模糊搜索
     */
    public function search_government()
    {

        $value = trim(input('get.value'));

        if ($value == 1 || $value == '1') {
            $order = 'a.time desc';
        } else if ($value == 2 || $value == '2') {
            $order = 'a.views desc';
        } else {
            $order = 'a.id';
        }

        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 5;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $keyword = trim(input('get.keyword'));
        $where['a.status'] = 1;
        $where['a.title|g.name|a.content'] = array('like', "%$keyword%");
        $join[0] = "__GOVERNMENT_TYPE__ g";
        $join[1] = "g.id = a.type";
        $res['government'] = db('government_work')->alias('a')->order($order)->where($where)->join($join[0], $join[1])->limit($limit)->page($page)->select();

        $res['count'] = db('government_work')->alias('a')->join($join[0], $join[1])->where($where)->count();

        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;

        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 店铺模糊搜索
     */
    public function search_store()
    {
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $value = trim(input('get.value'));

        if ($value == 1 || $value == '1') {
            $order = 'a.time desc';
        } else if ($value == 2 || $value == '2') {
            $order = 'a.shop_volume desc';
        } else {
            $order = 'a.id';
        }

        $keyword = trim(input('get.keyword'));
        $where1['a.static'] = 1;
        $where1['a.status'] = 2;
        $where1['a.name|s.shop_name'] = array('like', "%$keyword%");
        $join[0] = "__SHOP_CATEGORY__ s";
        $join[1] = "s.id = a.shop_id";
        $field1 = "a.id,a.username,a.name,a.shop_volume,a.image,a.scope,s.shop_name";
        $res['info'] = db('shop_information')->order($order)->alias('a')->field($field1)->join($join[0], $join[1])->where($where1)->limit($limit)->page($page)->select();
//        dump(db()->getLastSql());
        foreach ($res['info'] as $key => $value) {
            $trading_count = db('market_trading')->where('static', 1)->where('status', 1)->where('name', $value['id'])->count();
            $service_count = db('service_market')->where('state', 1)->where('s_id', $value['id'])->count();
            $res['info'][$key]['count'] = $trading_count + $service_count;
        }
        $res['count'] = db('shop_information')->alias('a')->join($join[0], $join[1])->where($where1)->count();

        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 课程模糊搜索
     */
    public function search_courses()
    {
        $value = trim(input('get.value'));
        if ($value == 1) {
            $order = 'a.time desc';
        } else if ($value == 2) {
            $order = 'a.views desc';
        } else {
            $order = 'a.id asc';
        }
        $where2['a.type'] = (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : array('neq', 0);
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 20;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $keyword = trim(input('get.keyword'));
        $where2['a.status'] = 1;
        $where2['a.title|v.name'] = array('like', "%$keyword%");
        $join[0] = "__VIDEO_TYPE__ v";
        $join[1] = "v.tid = a.type";
        $res['video'] = db('video_list')->alias('a')->join($join[0], $join[1])->where($where2)->order($order)->limit($limit)->page($page)->select();
        $res['count'] = db('video_list')->alias('a')->join($join[0], $join[1])->where($where2)->count();

        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 人才模糊搜索
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search_persons()
    {
        $where3['area'] = (isset($_GET['area']) && !empty($_GET['area'])) ? $_GET['area'] : array('neq', 0);
        $keyword = trim(input('get.keyword'));
        $workplace = trim(input('get.workplace'));
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 18;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $value = trim(input('get.value'));
        if ($value == 1) {
            $order = 'time desc';
        } else if ($value == 2) {
            $order = 'id desc';
        } else {
            $order = 'id asc';
        }

        $join[0] = "__TALENT_TYPE__ t";
        $join[1] = "t.id = a.talentrole";
        $where3['a.status'] = 1;
        $where3['a.sheet'] = 1;
        $where3['a.talentrole'] = (isset($_GET['label']) && !empty($_GET['label'])) ? $_GET['label'] : array('neq', 0);
        $where3['a.talentname|t.rolename'] = array('like', "%$keyword%");
        $where3['a.workplace'] = array('like', "%$workplace%");
        $filed = 'a.*,t.rolename';
        $res['personnel'] = db('talent')->order($order)->alias('a')->field($filed)->join($join[0], $join[1])->limit($limit)->page($page)->where($where3)->select();
        $res['count'] = db('talent')->alias('a')->join($join[0], $join[1])->where($where3)->count();
        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 师资模糊搜索
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search_teachers()
    {
        $where3['area'] = (isset($_GET['area']) && !empty($_GET['area'])) ? $_GET['area'] : array('neq', 0);
        $keyword = trim(input('get.keyword'));
        $workplace = trim(input('get.workplace'));
        $field = trim(input('get.field'));
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 18;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $where3['talentrole'] = (isset($_GET['label']) && !empty($_GET['label'])) ? $_GET['label'] : array('neq', 0);

        $value = trim(input('get.value'));
        if ($value == 1) {
            $order = 'time desc';
        } else if ($value == 2) {
            $order = 'id desc';
        } else {
            $order = 'id asc';
        }

        $join[0] = "__TALENT_TYPE__ t";
        $join[1] = "t.id = a.talentrole";
        $where3['a.status'] = 1;
        $where3['a.sheet'] = 2;
        $fields = 'a.*,t.rolename';
        $where3['a.talentname|t.rolename'] = array('like', "%$keyword%");
        $where3['a.workplace'] = array('like', "%$workplace%");
        $where3['a.field'] = array('like', "%$field%");
        $res['personnel'] = db('talent')->order($order)->alias('a')->join($join[0], $join[1])->field($fields)->limit($limit)->page($page)->where($where3)->select();
        $res['count'] = db('talent')->alias('a')->join($join[0], $join[1])->where($where3)->count();
        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 人才和师资的地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_province()
    {
        $res = db('province')->select();
        $this->_jsonp('200', 'OK', $res);
    }

    /**
     * 交易商品模糊搜索
     */
    public function search_trade()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $keyword = trim(input('get.keyword'));
        $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
        $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

        if ($maxmoney) {
            $where['a.store'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
        } else {
            $where['a.store'] = array('egt', $minmoney);
        }

        $where['a.c_id'] = (isset($_GET['c_id']) && !empty($_GET['c_id'])) ? $_GET['c_id'] : array('neq', 0);
        $where['a.category_id'] = (isset($_GET['category_id']) && !empty($_GET['category_id'])) ? $_GET['category_id'] : array('neq', 0);
        $where['a.status'] = 1;
        $where['a.static'] = 2;
        $where['a.is_show'] = 1;
        $where['a.username|a.content|m.market_name'] = array('like', "%$keyword%");
        $where['c.static'] = 1;
        $where['c.status'] = 2;


        $value = trim(input('value'));
        if ($value == 1 || $value == '1') {
            $order = 'value desc';
        } else if ($value == 2 || $value == '2') {
            $order = 'time';
        } else if ($value == 4 || $value == '4') {
            $order = 'store';
        } else if ($value == 5 || $value == '5') {
            $order = 'store desc';
        } else {
            $order = 'id';
        }

        $join[0] = "__SHOP_INFORMATION__ c";
        $join[1] = 'a.name = c.id';
        $join1[0] = "__MARKET_TRADING_CATEGORY__ m";
        $join1[1] = 'm.id = a.category_id';
        $field = "a.id,a.name as s_id,a.value,a.store,a.username,a.time,a.image,c.name,c.static,c.status";
        $alias = 'a';
        $res['market'] = db('market_trading')->alias($alias)->field($field)->join($join[0], $join[1])->join($join1[0], $join1[1])->where($where)->order($order)->limit(20)->page($page)->select();
        $res['count'] = db('market_trading')->alias($alias)->field($field)->join($join[0], $join[1])->join($join1[0], $join1[1])->where($where)->count();
        //分页
        $Page = new \app\api\model\Page($res['count'], 20);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }

    /**
     * 服务商品模糊搜索
     */
    public function search_service()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $keyword = trim(input('get.keyword'));
        $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
        $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

        if ($maxmoney) {
            $where['w.price'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
        } else {
            $where['w.price'] = array('egt', $minmoney);
        }

        $value = trim(input('value'));
        if ($value == 1 || $value == '1') {
            $order = 'value desc';
        } else if ($value == 2 || $value == '2') {
            $order = 'id';
        } else if ($value == 3 || $value == '3') {
            $order = 'time';
        } else if ($value == 4 || $value == '4') {
            $order = 'store';
        } else if ($value == 5 || $value == '5') {
            $order = 'store desc';
        } else {
            $order = 'id';
        }
        $where['w.type'] = (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : array('neq', 0);
        $where['w.state'] = 1;
        $where['w.status'] = 1;
        $where['w.goods_name|w.content|s.t_name'] = array('like', "%$keyword%");
        $where['i.static'] = 1;
        $where['i.status'] = 2;

        $join[0] = '__SHOP_INFORMATION__ i';
        $join[1] = 'w.s_id=i.id';
        $join1[0] = '__SERVICE_TYPE__ s';
        $join1[1] = 's.id = w.classify';
        $fields = 'w.id,w.rate,w.s_id,w.volume as value,w.price as store,w.goods_name as username,w.add_time as time,w.pic as image,i.name';
        $res['market'] = db('service_market')->where($where)->order($order)->limit(20)->page($page)
            ->alias('w')->field($fields)->join($join[0], $join[1])->join($join1[0], $join1[1])->select();
        $res['count'] = db('service_market')->where($where)->alias('w')->field($fields)->join($join[0], $join[1])->join($join1[0], $join1[1])->count();
        //分页
        $Page = new \app\api\model\Page($res['count'], 20);
        $render = $Page->render;

        $res['render'] = $render;
        $this->_jsonp('200', '成功', $res);
    }
}

